package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{
    //创建静态学生数组用于保存学生信息
    private static ArrayList<Student> students = new ArrayList<>();
    static {
        students.add(new Student("001","张三","20","2000-11-11"));
        students.add(new Student("002","张小三","21","1999-11-11"));
        students.add(new Student("003","张老三","19","2001-11-11"));
    }
    public boolean addStudent(Student stu) {
       return students.add(stu);
    }

    //创建findAllStudent()方法,将Student[]数组返回.
    public Student[] findAllStudent() {
        Student[] studentArray = new Student[students.size()];
        for (int i = 0; i < students.size(); i++) {
            studentArray[i] = students.get(i);
        }
        return studentArray;
    }

    public boolean deleteStudent(String id) {
        int index = getIndex(id);
        students.remove(index);
        return true;
    }

    public boolean updateStudent(Student stu) {
        int index = getIndex(stu.getId());
        students.set(index,stu);
        return true;
    }
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)){
                index = i;
            }
        }
        return index;
    }
}
